package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.enatity.Weibo;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping(value = "/v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;

    /**
     *发微博功能
     */
    @PostMapping(value = "publish")
    public int publish(@RequestBody WeiboDTO weiboDTO, HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return 2;
        }

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeiboContent(weibo);
        return 1;
    }
}
